<template>
    <div class="main-content">
        <TableLayout form-title="">
            <div slot="form-title" class="table-title"></div>
            <el-form :model="query" ref="query" slot="form" :rules="rules" label-width="80px" label-position="right">
                <el-row :gutter="41">
                    <el-col :span="8">
                        <el-form-item label="充值日期"> 
                            <el-row>
                                <el-col :span="11">
                                    <el-date-picker v-model="query.startDate" :clearable='true' :editable="false" type="date" placeholder="选择开始日期" :picker-options="pickerOptions1" @change="startDateChange" style="width:100%">
                                    </el-date-picker>
                                </el-col>
                                <el-col :span="2" align="center">-</el-col>
                                <el-col :span="11">
                                    <el-date-picker v-model="query.endDate" :clearable='true' :editable="false" type="date" placeholder="选择结束日期" :picker-options="pickerOptions2"  @change="endDateChange" style="width:100%">
                                    </el-date-picker>
                                </el-col>
                            </el-row>              
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="8">
                        <el-form-item label="省份" prop="province">
                            <province v-model="query.province"></province>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="市区" prop="city">
                            <city v-model="query.city" :province="query.province"></city>
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="8">
                        <el-form-item label="类别">
                            <el-select v-model="query.type" placeholder="请选择" style="width:100%;">
                                <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="支付方式">
                            <el-select v-model="query.payType" placeholder="请选择" style="width:100%;">
                                <el-option v-for="item in payTypeList" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="41">
                    <!-- <el-col :span="8">
                        <el-form-item label="区县" prop="county">
                            <region v-model="query.county" :city="query.city"></region>
                        </el-form-item>
                    </el-col> -->
                    <!-- <el-col :span="8">
                        <el-form-item label="类别">
                            <el-select v-model="query.type" placeholder="请选择" style="width:100%;">
                                <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="支付方式">
                            <el-select v-model="query.payType" placeholder="请选择" style="width:100%;">
                                <el-option v-for="item in payTypeList" :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="8">
                        <el-form-item label="e学云ID号" prop="eCode">
                            <el-input v-model="query.eCode" placeholder="请输入" style="width:100%" :maxlength="10"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="手机号" prop="phone">
                            <el-input v-model="query.phone" placeholder="请输入" style="width:100%" :maxlength="11"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="订单号" prop="orderNumber">
                            <el-input v-model="query.orderNumber" placeholder="请输入" style="width:100%"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <!-- <el-row :gutter="41">
                    <el-col :span="8">
                        <el-form-item label="手机号" prop="phone">
                            <el-input v-model="query.phone" placeholder="请输入" style="width:100%" :maxlength="11"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="订单号" prop="orderNumber">
                            <el-input v-model="query.orderNumber" placeholder="请输入" style="width:100%"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row> -->
                <div class="top-line">
                    <el-button type="primary" class="top-btn" @click="onSubmit('query')">查询</el-button>
                </div>
            </el-form>
            <div class="table-title" slot="table-title">
                <div class="btnPosition">
                    <el-button @click="exportTable()">导出</el-button>
                </div>
            </div>
            <el-table :data="tableData" border v-loading="loading" style="width: 100%">
                <el-table-column label="充值时间" width="170" align="center">
                    <template scope="scope">
                        <span>{{scope.row.date | isNull}}</span>
                    </template>
                </el-table-column>
    
                <el-table-column label="类别" align="center">
                    <template scope="scope">
                        <span>{{scope.row.type | isNull}}</span>
                    </template>
                </el-table-column>
    
                <el-table-column label="e学云ID号" align="center">
                    <template scope="scope">
                        <span>{{scope.row.eCode | isNull}}</span>
                    </template>
                </el-table-column>
    
                <el-table-column label="姓名（用户名）" align="center">
                    <template scope="scope">
                        <span>{{scope.row.userName | isNull}}</span>
                    </template>
                </el-table-column>

                <el-table-column label="手机号" align="center">
                    <template scope="scope">
                        <span>{{scope.row.phone | isNull}}</span>
                    </template>
                </el-table-column>

                <el-table-column label="所属地区" align="center">
                    <template scope="scope">
                        <span>{{scope.row.adress=='null_null_null'?'--':scope.row.adress}}</span>
                    </template>
                </el-table-column>

                <el-table-column label="充值金额（元）" align="center">
                    <template scope="scope">
                        <span>{{ !scope.row.rechangeCash?'--':Number(scope.row.rechangeCash).toFixed(2) }}</span>
                    </template>
                </el-table-column>

                <el-table-column label="支付方式" align="center">
                    <template scope="scope">
                        <span>{{scope.row.payType | isNull}}</span>
                    </template>
                </el-table-column>

                <el-table-column label="订单号" align="center">
                    <template scope="scope">
                        <span>{{scope.row.orderNumber | isNull}}</span>
                    </template>
                </el-table-column>

                <el-table-column label="操作">
                    <template scope="scope">
                         <el-button type="text" class="btn-success" @click="searchDetail(scope.row)">查看余额明细</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination layout="total, sizes, prev, pager, next, jumper" :total="pageQuery.total" :current-page.sync="pageQuery.currentpage" :page-sizes="config.pageSizes" :page-size="config.pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange">
                </el-pagination>
            </div>
        </TableLayout>
    </div>
</template>

<script>
    import moment from 'moment';
    import Levelbar from '../../layout/Levelbar';
    import TableLayout from 'components/TableLayout';
    import province from 'components/CascadeRegion/province';
    import city from 'components/CascadeRegion/city';
    import region from 'components/CascadeRegion/region';
    import { queryRecharge } from 'api/integralManage';
    import { mapState, mapActions } from 'vuex';
    import validate from '@/utils/validate';

    export default {
        name: 'recordTable',
        components: { Levelbar, TableLayout, province, city, region },
        data() {
            return { 
                loading: true,  
                tableData: [],
                pageQuery: {
                    currentpage: 1,
                    total: 0
                },
                query: {
                    startDate: '',
                    endDate: '',
                    eCode: '', 
                    phone: '', 
                    province: '',
                    city: '',
                    county: '',
                    orderNumber: '', 
                    type: '', 
                    payType: ''   
                },
                typeList: [{ 
                    value: '',
                    label: '请选择'
                }, {
                    value: '40003',
                    label: '积分充值'
                }, {
                    value: '40001',
                    label: '余额充值'
                }],
                payTypeList: [{
                    value: '',
                    label: '请选择'
                }, {
                    value: '0',
                    label: '微信'
                }, {
                    value: '1',
                    label: '支付宝'
                }, {
                    value: '2',
                    label: 'e学云余额'
                }],
                pickerOptions1: {
                    disabledDate(time) {
                        return (time.getTime()) > (Date.now());
                    }
                },
                pickerOptions2: {
                    disabledDate(time) {
                        return (time.getTime()) > (Date.now());
                    }
                },
                rules: {
                    eCode: [...validate.exueCode()],
                    phone: [...validate.phone()]
                
                }
            }
        },
        created() {
            this.queryData();
        },
        computed: {
            ...mapState({
                config: state => state.config.table
            })
        },
        methods: {
            ...mapActions(['ChangePageSize']),
            handleSizeChange(val) {
                this.pageQuery.currentpage = 1;
                this.ChangePageSize(val);
                this.queryData();
            },
            handleCurrentChange(val) {
                this.pageQuery.currentpage = val;
                this.queryData();
            },
            onSubmit(ruleForm) {
                this.$refs[ruleForm].validate((valid) => {
                    if (valid) {
                        this.pageQuery.currentpage = 1;
                        this.queryData();
                    } else {
                        return false;
                    }
                });
            },
            queryData() {
            
                let eCode = this.query.eCode; // e学账号（可模糊查询）
                let phone = this.query.phone; // 手机号
                let provinceId = this.query.province; // 省id
                let cityId = this.query.city; // 市id
                let areaId = this.query.county; // 区县id
                let dataStart = this.query.startDate==''?'':moment(this.query.startDate).format('YYYY-MM-DD'); // 查询开始时间
                let dataEnd = this.query.endDate==''?'':moment(this.query.endDate).format('YYYY-MM-DD'); // 查询结束时间 
                let orderNumber = this.query.orderNumber; // 订单号
                let type = this.query.type; // 充值类型（40001:充值, 40003:购买积分）
                let payType = this.query.payType; // 0:微信 1：支付宝  2:e学账户         

                queryRecharge(
                    this.pageQuery.currentpage,
                    this.config.pageSize,
                    eCode, 
                    phone, 
                    provinceId, 
                    cityId,
                    areaId, 
                    dataStart, 
                    dataEnd,
                    orderNumber, 
                    type, 
                    payType   
                ).then(response => {
                    this.loading = false;
                    if (response.data.content) {
                        this.tableData = response.data.content.list;
                        this.pageQuery.total = response.data.content.pagination.total; 
                    } else {
                        this.tableData = [];
                    }
                    
                }).catch(() => {
                    this.loading = false;
                })
            },
            startDateChange(date) {
                this.pickerOptions2 = {
                    disabledDate(time) {
                        return (time.getTime() < (new Date(date).getTime() - 864e5)) || (time.getTime() > new Date().getTime());
                    }
                }
            },
            endDateChange(date) {
                if (date) {
                    this.pickerOptions1 = {
                        disabledDate(time) {
                            return (time.getTime() > new Date(date).getTime());
                        }
                    } 
                } else {
                    this.pickerOptions1 = {
                        disabledDate(time) {
                            return (time.getTime()) > (Date.now());
                        }
                    }
                }
            },

            // 导出
            exportTable() {
                let eCode = this.query.eCode; // e学账号（可模糊查询）
                let phone = this.query.phone; // 手机号
                let provinceId = this.query.province; // 省id
                let cityId = this.query.city; // 市id
                let areaId = this.query.county; // 区县id
                let dataStart = this.query.startDate==''?'':moment(this.query.startDate).format('YYYY-MM-DD'); // 查询开始时间
                let dataEnd = this.query.endDate==''?'':moment(this.query.endDate).format('YYYY-MM-DD'); // 查询结束时间 
                let orderNumber = this.query.orderNumber; // 订单号
                let type = this.query.type; // 充值类型（40001:充值, 40003:购买积分）
                let payType = this.query.payType; // 0:微信 1：支付宝  2:e学账户         
                window.location.href = `${LOCAL_API.cm}/point/manage/outExcelByRecharge?eCode=${eCode}&phone=${phone}&provinceId=${provinceId}&cityId=${cityId}&areaId=${areaId}&dataStart=${dataStart}&dataEnd=${dataEnd}&orderNumber=${orderNumber}&type=${type}&payType=${payType}`;
            },

            // 查看余额明细
            searchDetail(obj) {
                this.$router.push({path: `/accountExUser/checkFigure/${obj.userId}`});
            }

        }

    }
</script>

<style lang="scss" scoped>

</style>

